package Classes
{
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;

	public class WorldCell extends EventDispatcher
	{
		private var m_world:World;
		private var m_position:Point;
		private var m_spawnTimer:Timer;
		private var m_entitySpawned:Entity;
		private var m_spawnRule:SpawnRule;
		
		public function WorldCell(world:World, position:Point)
		{
			m_position = position;
			m_world = world;
		}
		
		public function setSpawnRule(spawnRule:SpawnRule):void{
			m_spawnRule = spawnRule;
			if(!m_entitySpawned){
				m_spawnTimer = new Timer(spawnRule.spawnTime,0);
				m_spawnTimer.addEventListener(TimerEvent.TIMER, onSpawnTimeComplete);
				m_spawnTimer.start();
			}
		}
		
		private function onSpawnTimeComplete(te:TimerEvent):void{
			var mobs:Array = m_world.entityManager.getMobs();
			var rand:int = Util.secureRand(0,mobs.length-1);
			var mob:Object = mobs[rand];
						
			m_entitySpawned = m_world.entityManager.createMob(mob.config.attributes['name'],m_position);
			m_spawnTimer.stop();
		}
	}
}